package cn.edu.hunau.oop.day07;

import org.junit.Test;

import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;

/**
 * @author hb
 * @date 2025-08-12 09:18:44
 */
public class DateHwTest {

    @Test
    public void tsetParseId(){
         System.out.println(parseId("430111200208120012"));
    }


    public int parseId(String id){
        //格式校验
        String idRegex="^\\d{17}[0-9Xx]$";
        if(!id.matches(idRegex)){
            throw new RuntimeException("身份证格式不正确");
        }
        //获得日期 430111200208120012
        //解析出身时间
        String birth = id.substring(6, 14);//20020812
        //日期的计算
        //字符串转LocalDate
        LocalDate birthDate=LocalDate.parse(birth, DateTimeFormatter.ofPattern("yyyyMMdd"));//2002-08-12

        //获得现在的时间
        LocalDate now = LocalDate.now();

        //获得今年生日的时间

        LocalDate birthday = birthDate.withYear(now.getYear());

        //  int age= now.getYear()-birthDate.getYear();
        //  if(birthday.isAfter(now)){
        //    age--;
        // }

        Period period = Period.between(birthDate, now);

        int age = period.getYears();
        return age;
    }
}
